/***
 * author: Adam Pociejowski
 */

package requests.music;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import requests.IRequest;
import requests.settings.Settings;

public class Mp3PlaylistQueue implements IRequest, ActionListener {
	
	private int limit;
	public JFrame frame;
	private JPanel panel;
	private GridBagConstraints c;
	private JButton buttons[];
	private Mp3Random mp3Random;
	private Font font = new Font("Times New Roman", 1, 16);
	private Color backgroundColor, buttonsColor, activeColor;
	
	public Mp3PlaylistQueue(int heigth, int width, int limit, Mp3Random mp3Random, Settings settings) {

		this.limit = limit;
		this.mp3Random = mp3Random;
		

		String content = settings.getSettings("mp3PlaylistQueueBackgroundColor");
		backgroundColor = getColor(content);
		content = settings.getSettings("mp3PlaylistQueueButtonsColor");
		buttonsColor = getColor(content);
		content = settings.getSettings("mp3PlaylistQueueActiveColor");
		activeColor = getColor(content);
		
		frame = new JFrame();
		frame.setSize(width, heigth);
		frame.setLocation(1050,200);
		frame.setTitle("D.E.M.I - Mp3 Playlist Queue");
		frame.setFocusableWindowState(true);
		frame.setResizable(false);
		
		GridBagLayout layout = new GridBagLayout();
        layout.columnWidths = new int[] { width - 16 };
        
		panel = new JPanel(layout);
		panel.setBackground(backgroundColor);
		c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		frame.add(panel);
	}
	
	public void paint(String[] songs) {

		frame.setFocusableWindowState(false);
		
		frame.setVisible(true);	

		if (buttons == null) {
			buttons = new JButton[limit];
	        c.weightx = 0;
	        c.weighty = 1;
	        c.gridwidth = 2;
	        c.insets = new Insets(3,3,3,3);
	        
	        
			for (int i = 0; i < songs.length; i++) {
				if (i > 0) 
					c.insets = new Insets(0,3,3,3);
				buttons[i] = new JButton(songs[i]);
				buttons[i].setHorizontalAlignment(SwingConstants.LEFT);
				buttons[i].setBackground(buttonsColor);
				buttons[i].setFont(font);
				buttons[i].setOpaque(true);
				buttons[i].addActionListener(this);
				c.gridy = i;
				panel.add(buttons[i], c);
			}
			buttons[0].setBackground(activeColor);
		}
		else {
			for (int i = 0;i < songs.length; i++) {
				buttons[i].setText(songs[i]);
			}
		}
		frame.setFocusableWindowState(true);
	}
	
	private Color getColor(String content) {
		int tmp[] = new int[3];
		tmp[0] = Integer.parseInt(content.substring(0,3));
		tmp[1] = Integer.parseInt(content.substring(4,7));
		tmp[2] = Integer.parseInt(content.substring(8,11));
		Color color = new Color(tmp[0],tmp[1],tmp[2]);
		return color;
	}

	@Override
	public void returnDEMI(String text) { 
		System.out.println(text);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		/********   Changing song after click on JButton and delegate       * 
		 * 			title to commanding playlist class               ********/
		
		String src = ""+e.getSource();
		int index = src.indexOf("text");
		if (index == -1) {
			returnDEMI("Mp3Browser - action Performed Error");
		}
		else {
			String title = src.substring(index+5, src.length() - 21);
			mp3Random.nextOnClick(title);
			
		}	
		
		/*********************************************************************/
	}

}
